import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/productDataManagement',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'RedirectWrap',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },

  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/personal-center',
    name: 'Personal',
    meta: {
      title: t('router.personal'),
      hidden: true,
      canTo: true
    },
    children: [
      {
        path: 'personal-center',
        component: () => import('@/views/Personal/PersonalCenter/PersonalCenter.vue'),
        name: 'PersonalCenter',
        meta: {
          title: t('router.personalCenter'),
          hidden: true,
          canTo: true
        }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  },

]
export const asyncRouterMap: AppRouteRecordRaw[] = [




 {
    path: '/productDataManagement',
    component: Layout, // 使用 Layout 组件
    name: 'ProductDataManagement',
    redirect: '/productDataManagement/index',
    meta: {
      icon: 'vi-ant-design:dashboard-filled',
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/productDataManagement/index.vue'), // 正确的导入方式
        name: 'ProductDataManagementDemo',
        meta: {
          title: '产品数据管理',
          icon: 'iconfont icon-chanpinshujuguanli-01'
        }
      }
    ]
  },



   {
    path: '/collectionTaskManagement',
    component: Layout, // 使用 Layout 组件
    name: 'CollectionTaskManagement',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/collectionTaskManagment/index.vue'), // 正确的导入方式
        name: 'CollectionTaskManagementDemo',
        meta: {
          title: '采集任务管理',
          icon: 'iconfont icon-caijirenwuguanliqi'
        }
      }
    ]
  },
  {
    path: '/equipmentManagement',
    component: Layout, // 使用 Layout 组件
    name: 'EquipmentManagement',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/EquipmentManagement/index.vue'), // 正确的导入方式
        name: 'EquipmentManagementDemo',
        meta: {
          title: '型号设备树管理',
          icon: 'iconfont icon-shebeijiankong'
        }
      }
    ]
  },
  {
    path: '/companyManagement',
    component: Layout, // 使用 Layout 组件
    name: 'CompanyManagement',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/CompanyManagement/index.vue'), // 正确的导入方式
        name: 'CompanyManagementDemo',
        meta: {
          title: '成品单位管理',
          icon: 'iconfont icon-jianzhuqizhongjixieshebeishiyongdengji-01'
        }
      }
    ]
  },
  {
    path: '/catalogManagement',
    component: Layout, // 使用 Layout 组件
    name: 'CatalogManagement',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/CatalogManagement/index.vue'), // 正确的导入方式
        name: 'CatalogManagementDemo',
        meta: {
          title: '优选目录管理',
          icon: 'iconfont icon-a-zhidu14'
        }
      }
    ]
  },
  {
    path: '/emptyPacketManagement',
    component: Layout, // 使用 Layout 组件
    name: 'EmptyPacketManagement',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/emptyPacketManagement/index.vue'), // 正确的导入方式
        name: 'EmptyPacketManagementDemo',
        meta: {
          title: 'WKB管理',
          icon: 'iconfont icon-delivered'
        }
      }
    ]
  },
  // {
  //   path: '/authorization',
  //   component: Layout,
  //   redirect: '/authorization/User/index',
  //   name: 'Authorization',
  //   meta: {

  //   },
  //     children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/Authorization/User/index.vue'), // 正确的导入方式
  //       name: 'UserManagementDemo',
  //       meta: {
  //         title: '用户管理',
  //         icon: 'iconfont icon-yonghuguanli'
  //       }
  //     }


  //   ]

  //   },
]
// 合并所有路由
const allRoutes = [...constantRouterMap, ...asyncRouterMap] as RouteRecordRaw[]
const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: allRoutes, // 使用合并后的路由
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
